package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.requests.UserPageRequst;
import com.example.springboot.entity.User;
import com.example.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;
    @GetMapping("/list")
    public Result list() {
        List<User> users=userService.list();
        return  Result.success(users);
    }
    @PostMapping("/save")
    public  Result save( @RequestBody User user)
    {
        userService.save(user);
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(UserPageRequst userPageRequst) {
        return  Result.success(userService.page(userPageRequst));
    }
    @GetMapping("/{id}")
    public Result getByid(@PathVariable Integer id)
    {
         User users=userService.getByid(id);
         System.out.println(users);
        return Result.success(users);
    }
    @PutMapping("/update")
    public  Result update( @RequestBody User user)
    {
        userService.update(user);
        return Result.success();
    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Result delete(@PathVariable Integer id)
    {
        userService.delete(id);
        return Result.success();
    }

}
